package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.config.QueryMD;
import org.jdesktop.swingx.JXTable;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * User: ukman
 * Date: 20.07.2011
 * Time: 15:11:18
 */
public class DefaultGridFactory implements GridFactory {

    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void createContent(Container container, final QueryMD queryMD) {
        container.removeAll();

        final QueryTableModel tableModel = (QueryTableModel) Context.getBeanFactory().getBean("dbTableModel");
        tableModel.setQuery(queryMD);

        final JXTable table = new StrippedTable(tableModel);
        GridComparator comparator = new GridComparator();

        int count = table.getColumnCount(true);
        for (int i = 0; i < count; i++) {
            table.getColumnExt(i).setComparator(comparator);
        }

        table.setColumnControlVisible(true);
        table.getTableHeader().setVisible(true);
        container.add(new JScrollPane(table));

        final EventDispatcher eventDispatcher = (EventDispatcher) Context.getBeanFactory().getBean("eventDispatcher");
        eventDispatcher.addEventListener(UIEvent.REFRESH, new IEventListener() {
            @Override
            public void handleEvent(IEvent event) {
                if (table.isShowing()) {
                    DBTableModel dbTableModel = (DBTableModel) tableModel;
                    dbTableModel.refresh();
                    dbTableModel.fireTableDataChanged();
                }
            }
        });

        if (queryMD.getEntityName() != null) {
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() > 1) {
                        editRow(table, queryMD);
                    }
                }
            });
            table.addKeyListener(new KeyAdapter() {
                @Override
                public void keyTyped(KeyEvent e) {
                    if (e.getKeyChar() == KeyEvent.VK_SPACE) {
                        editRow(table, queryMD);
                    }
                }
            });
        }
    }

    protected void editRow(JTable table, QueryMD queryMD) {
        int row = table.getSelectedRow();
        row = table.convertRowIndexToModel(row);
        QueryTableModel tableModel = (QueryTableModel) table.getModel();
        Object entityId = tableModel.getValue(row, "id");
        if (entityId != null) {
            final EventDispatcher eventDispatcher = (EventDispatcher) Context.getBeanFactory().getBean("eventDispatcher");
            eventDispatcher.sendEvent(new EditEvent(queryMD.getEntityName(), entityId));
        }

    }
}
